<template>
  <div class="app" style="">
    <div class="featured-content"
         :style="backgroundStyle">
      <img class="featured-title" :src="featureLogo" alt="">
      <p class="featured-desc">
        在远离大都会的小山村，住着巫女世家出身的高中女孩宫水三叶。不知从何时起，三叶在梦中就会变成一个住在东京的高中男孩。那里有陌生的同学和朋友，有亲切的前辈和繁华的街道，一切都是如此诱人而真实。另一方面，住在东京的高中男孩立花泷则总在梦里来到陌生的小山村，以女孩子的身份过着全新的生活。许是受那颗神秘彗星的影响，立花和三叶在梦中交换了身份。他们以他者的角度体验着对方的人生，这期间有愤怒、有欢笑也有暖心。只是两人并不知道，身份交换的背后隐藏着重大而锥心的秘密……
      </p>
      <router-link :to="'/film/info?fid=' + 9">
        <button class="featured-button">在线观看</button>
      </router-link>
    </div>

    <div class="movie-list-container">
      <h1 class="movie-list-title">最近热映</h1>
      <div class="movie-list-wrapper">
        <div class="movie-list" ref="movieList">
          <div v-for="item in recentFilmList" :key="item.id" class="movie-list-item">
            <div class="poster">
              <el-tag
                  class="poster-tag"
                  :type="item.vip ? 'warning' : 'success'"
                  size="small">
                {{ item.vip ? 'VIP' : '免费' }}
              </el-tag>
              <img class="movie-list-item-img" :src="item.cover" alt="">
            </div>
            <span class="movie-list-item-title">{{ item.name }}</span>
            <p class="movie-list-item-desc">{{ item.introduction }}</p>
            <router-link :to="'/film/info?fid=' + item.id">
              <button class="movie-list-item-button">观看</button>
            </router-link>

            <span class="movie-release-time">
              {{ item.releaseTime }} 上映
            </span>
          </div>
        </div>
        <i class="fas fa-chevron-right arrow" @click="scrollRight(1)"></i>
      </div>
    </div>

    <div class="movie-list-container">
      <h1 class="movie-list-title">今日推荐</h1>
      <div class="movie-list-wrapper">
        <div class="movie-list" ref="movieList2">
          <div v-for="item in recommendFilmList" :key="item.id" class="movie-list-item">
            <div class="poster">
              <el-tag
                  class="poster-tag"
                  :type="item.vip ? 'warning' : 'success'"
                  size="small">
                {{ item.vip ? 'VIP' : '免费' }}
              </el-tag>
              <img class="movie-list-item-img" :src="item.cover" alt="">
            </div>
            <span class="movie-list-item-title">{{ item.name }}</span>
            <p class="movie-list-item-desc">{{ item.introduction }}</p>
            <router-link :to="'/film/info?fid=' + item.id">
              <button class="movie-list-item-button">观看</button>
            </router-link>

            <span class="movie-release-time">
              {{ item.releaseTime }} 上映
            </span>
          </div>
        </div>
        <i class="fas fa-chevron-right arrow" @click="scrollRight(2)"></i>
      </div>
    </div>

  </div>
</template>

<script>
import {ListAllFilm, GetFilmByPage} from "@/api/film";


export default {
  props: {
    item: Object
  },

  data() {
    return {
      clickCounter: 0,
      filmList: [],
      recentFilmList: [],
      recommendFilmList: [],
      backgroundStyle: {
        background: `linear-gradient(to bottom, rgba(0,0,0,0), #1f1c30), url(${require('@/assets/img/feature.png')})`,
        backgroundSize: 'cover',
        backgroundRepeat: 'no-repeat',
        backgroundPosition: 'center 20%'
      },
      featureLogo: require('@/assets/img/feature_logo.png')
    }
  },

  mounted() {
    this.fetchFilmList();
  },
  methods: {
    scrollRight(row) {
      let movieList = this.$refs.movieList;
      if (row === 2) {
        movieList = this.$refs.movieList2;
      }
      const itemNumber = movieList.querySelectorAll("img").length;
      const ratio = Math.floor(window.innerWidth / 270);
      this.clickCounter++;

      if (itemNumber - (4 + this.clickCounter) + (4 - ratio) >= 0) {
        movieList.style.transform = `translateX(${
            movieList.computedStyleMap().get("transform")[0].x.value - 300
        }px)`;
      } else {
        movieList.style.transform = "translateX(0)";
        this.clickCounter = 0;
      }
    },
    fetchFilmList() {
      ListAllFilm().then(res => {
        this.filmList = res.data;
        GetFilmByPage("本周", 1, 8).then(res => {
          this.recentFilmList = res.data.items.slice(0, 7);
        })
        const today = new Date().toISOString().slice(0, 10); // e.g., "2024-05-24"
        const seed = Number(today.split('-').join(''));
        this.recommendFilmList = this.getRecommendations(seed, this.filmList, 7);
        console.log(this.recommendFilmList);
      });
    },
    getRecommendations(seed, filmList, count) {
      let random = () => {
        seed = (seed * 9301 + 49297) % 233280;
        return seed / 233280;
      };

      let selectedFilms = [];
      let filmListCopy = [...filmList];

      for (let i = 0; i < count; i++) {
        if (filmListCopy.length === 0) break;
        const randomIndex = Math.floor(random() * filmListCopy.length);
        selectedFilms.push(filmListCopy.splice(randomIndex, 1)[0]);
      }

      return selectedFilms;
    }

  }
}
</script>

<style scoped>

.app {
  padding: 0px 0px;
}

.featured-content {
  height: 60vh;
  padding: 50px;
}

.featured-title {
  width: 500px;
  margin-top: 60px;
}

.featured-desc {
  font-family: "PingFang SC";
  width: 500px;
  color: lightgray;
  margin: 15px 0;
  font-size: 14px;
  text-indent: 2em;
}

.featured-button {
  background-color: #4dbf00;
  color: #e3e3e3;
  padding: 10px 20px;
  border-radius: 10px;
  border: none;
  outline: none;
  font-weight: bold;
  font-family: "PingFang SC";
  text-shadow: 2px 2px 2px #6f6f6f;;
  letter-spacing: 1px;
  transition: all 0.5s ease-in-out;
}

.movie-list-container {
  padding: 0 20px 30px 20px;
}

.movie-list-wrapper {
  position: relative;
  overflow: hidden;
}

.movie-list {
  display: flex;
  align-items: center;
  height: 250px;
  transform: translateX(0);
  transition: all 1s ease-in-out;
}

.movie-list-item {
  margin-right: 30px;
  position: relative;
}

.movie-list-item:hover .movie-list-item-img {
  transform: scale(1.2);
  margin: 0 30px;
  opacity: 0.5;
}

.movie-list-item:hover .movie-list-item-title,
.movie-list-item:hover .movie-list-item-desc,
.movie-list-item:hover .movie-list-item-button {
  opacity: 1;
}

.featured-button:hover,
.movie-list-item-button:hover {
  cursor: pointer;
  transition: all 0.5s ease-in-out;
  background-color: #b9e769;
  -webkit-box-shadow: 10px 10px 70px 6px rgba(185, 231, 105, 1);
  -moz-box-shadow: 10px 10px 70px 6px rgba(185, 231, 105, 1);
  box-shadow: 10px 10px 70px 6px rgba(185, 231, 105, 1);
}

.movie-list-item:hover .movie-release-time {
  opacity: 0;
}

.poster {
  position: relative;
}

.el-tag {
  position: relative;
  top: 20px;
  z-index: 5;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.movie-list-item-img {
  transition: all 1s ease-in-out;
  width: 270px;
  height: 200px;
  object-fit: cover;
  border-radius: 20px;
  top: 50px;
  overflow: hidden;
}

.movie-list-item-title {
  padding: 0 10px;
  font-size: 32px;
  font-weight: bold;
  font-family: "PingFang SC";
  color: #f0f0f0;
  position: absolute;
  top: 15%;
  left: 40px;
  opacity: 0;
  transition: 1s all ease-in-out;
}

.movie-list-item-desc {
  padding: 10px;
  font-size: 14px;
  font-family: "PingFang SC";
  color: #f0f0f0;

  position: absolute;
  top: 28%;
  left: 40px;
  width: 230px;
  height: 50px;
  opacity: 0;
  transition: 1s all ease-in-out;

  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
}

.movie-list-item-button {
  padding: 8px 15px;
  background-color: #4dbf00;
  color: #f0f0f0;
  text-shadow: 1px 1px 1px #6f6f6f;;
  font-family: "PingFang SC";

  border-radius: 7px;
  outline: none;
  border: none;
  cursor: pointer;
  position: absolute;
  bottom: 40px;
  left: 40px;
  opacity: 0;
  transition: 1s all ease-in-out;


}

.arrow {
  font-size: 120px;
  position: absolute;
  top: 50px;
  right: 0;
  color: lightgray;
  opacity: 0.7;
  cursor: pointer;
}

.movie-list-title {
  color: #f0f0f0;
  font-family: "PingFang SC";
  margin-bottom: 0;
}

.movie-release-time {
  color: lightgray;
  font-family: "PingFang SC";
  opacity: 1;
  transition: 0.6s all ease-in-out;
}


</style>